/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.dao.impl;

import java.math.BigInteger;
import java.util.List;
import mx.netbrain.dao.ProfileDao;
import mx.netbrain.model.Profile;
import mx.netbrain.util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author Julian
 */
public class ProfileDaoImpl implements ProfileDao {

    @Override
    public void update(Profile profile) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.merge(profile);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al actualizar perfil");
            session.beginTransaction().rollback();
        }
    }

    @Override
    public Profile findById(Long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Profile) session.load(Profile.class, id);
    }

    @Override
    public List<Profile> listAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("from Profile").list();
    }

    @Override
    public void save(Profile profile) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.persist(profile);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al actualizar perfil");
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void delete(Profile profile) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(profile);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al actualizar perfil");
            session.beginTransaction().rollback();
        }
    }
}
